package com.cbd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.CreateKeySecondPass;
import org.hibernate.transform.ResultTransformer;
import org.springframework.stereotype.Repository;

import com.cbd.dao.LandladyDao;
import com.cbd.pojo.Landlady;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.RentInfo;
import com.cbd.pojo.User;
@Repository
public class LandladyDaoImpl implements LandladyDao {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public int login(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int regist(Landlady landlady) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		return (int) session.save(landlady);
	}

	@Override
	public Landlady findLandLayById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Landlady> findAllNoPassLandLady() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateLandladyType(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Landlady> findAllPortNoPassLandLady() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Landlady findLadyByCard(String card) {
		Session session = sessionFactory.getCurrentSession();
		Query query =session.createQuery("from Landlady where ladyCard = ?");
		query.setParameter(0, card);
		return (Landlady) query.uniqueResult();
	}
	
	@Override
	public void publishRentInfo(RentInfo info) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(info);
	}

	@Override
	public int updateLandlady(Landlady landLady,int id) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		System.out.println(landLady.getLadyDescribe());
		Query query=session.createSQLQuery("update landlady l left join user u on l.userID=u.userID set u.userPWD=?,l.ladyTel=?,l.ladyAddress=?,l.ladyDescribe=? where l.ladyID=? ");
		query.setParameter(0,landLady.getUser().getUserPWD());
		query.setParameter(1, landLady.getLadyTel());
		query.setParameter(2, landLady.getLadyAddress());
		query.setParameter(3, landLady.getLadyDescribe());
		query.setParameter(4, id);
		int cout=query.executeUpdate();
		return cout;
	}

	@Override
	public Landlady showlandLady(int id) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery("from Landlady  where userID=?");
		query.setParameter(0, id);
		Landlady user=(Landlady) query.list().get(0);
		return user;
	}

	@Override
	public void updateRPById(int ladyID) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Landlady l = (Landlady) session.get(Landlady.class, ladyID);
		String [] rps = l.getLadyRP().split("/");
		int num = Integer.parseInt(rps[1]);
		num=num+1;
		String rp =rps[0]+"/"+num;
		//System.out.println(rp);
		l.setLadyRP(rp);
		Query query = session.createQuery("update Landlady l set l.ladyRP = ? where l.ladyID = ?");
		query.setString(0, rp);
		query.setInteger(1, ladyID);
		query.executeUpdate();
	}

	@Override
	public List<Landlady> findAllLandladyByLadyType(boolean ladyType) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Landlady where ladyType = ?");
		query.setParameter(0, ladyType);
		return query.list();
	}

	@Override
	public List<Object[]> findOnePageLandladyByLadyTypeAndPage(boolean ladyType, int pageSize, int currentPage) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		// ladyID   userName  ladyName  cardID  ladyTime提交时间   5个参数
		Query query = session.createQuery("select ll.ladyID,u.userName,ll.ladyName,ll.ladyCard,ll.ladyTime "
				+ " from Landlady ll,User u "
				+ " where ll.ladyType = ? and ll.user.userID = u.userID ");
		query.setParameter(0, ladyType);
		int beginItem = (currentPage-1)*pageSize;
		query.setFirstResult(beginItem);
		query.setMaxResults(pageSize);
		return query.list();
	}

	@Override
	public Landlady findLandladyByLadyID(int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Landlady where ladyID = ?");
		query.setParameter(0, ladyID);
		return (Landlady) query.list().get(0);
	}

	@Override
	public int updateLandladyTypeByLadyID(int ladyID, boolean ladyType) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("update Landlady ll set ll.ladyType = ? where "
				+ " ll.ladyID = ? ");
		query.setParameter(0, ladyType);
		query.setParameter(1, ladyID);
		return query.executeUpdate();
	}

	@Override
	public List<Landlady> findAllUnablePortByLadyID(int parseInt) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query query = session.createQuery("select distinct (l)  from Landlady l where l.ladyID in ( select p.landlady.ladyID from Personalcarport p where p.type= 0 and p.landlady.ladyID = ?)  ");
		query.setParameter(0, parseInt);
		return query.list();
	}
	@Override
	public List<Landlady> findAllUnAbelPort(){
		Session session=sessionFactory.getCurrentSession();
		Query query = session.createQuery("select distinct (l) from Landlady l join l.personalcarport as p  where l.ladyID in ( select p.landlady.ladyID from Personalcarport p where p.type= 0 ) ");
		return query.list();
	}
	@Override
	public PageBean findAllUnAbelPort(PageBean pageBean) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query query = session.createQuery("select distinct (l) from Landlady l join l.personalcarport as p  where l.ladyID in ( select p.landlady.ladyID from Personalcarport p where p.type= 0 ) ");
		query.setFirstResult((pageBean.getCurPage()-1)*pageBean.getPageSize());
		query.setMaxResults(pageBean.getPageSize());
		List<Landlady> list =findAllUnAbelPort();
		int size=list.size();
		pageBean.setCountPage(size%pageBean.getPageSize()==0?size/pageBean.getPageSize():size/pageBean.getPageSize()+1);
		pageBean.setList(query.list());
		return pageBean;
	}

	@Override
	public void deleteLandladyAndUser(Landlady lady) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete Landlady ll where ll.ladyID = ?");
		Query query1 = session.createQuery("delete User u where u.userID = ?");
		query.setParameter(0, lady.getLadyID());
		query1.setParameter(0, lady.getUser().getUserID());
		query.executeUpdate();
		query1.executeUpdate();
	}


}
